package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.gulimall.common.constant.AuthServerConstant;
import com.gulimall.common.vo.MemberEntityVo;
import com.atguigu.gulimall.auth.vo.SocialUser;
import com.gulimall.common.utils.HttpUtils;
import com.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
public class OAuth2Controller {
    @Autowired
    MemberFeignService memberFeignService;

    /**
     *           <li th:if="${session.loginUser} !=null">
     *            <a>欢迎，[[${session.loginUser.username}]]</a>
     * @param code
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping("/auth2.0/gitee/success")
    public String auth2Login(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据自己的信息设置value
        Map<String, String> map = new HashMap<>();
        map.put("grant_type","authorization_code");
        map.put("code",code);
        map.put("client_id","8d86c8eb7b7a426ddd15d1c59a6c9cf748b3a117ace6182b71e94b4ff6e4126d");
        map.put("redirect_uri","http://auth.gulimall.com/auth2.0/gitee/success");
        map.put("client_secret","b0c33115bd0b077ce612219886ff2e2060d08e6a32125b752bc003765cc1bd4c");
        //发送请求，得到数据，HttpUtils是我自己的一个工具类，用来发送get、post请求的
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(),map);

        //得到access_token
        if(response.getStatusLine().getStatusCode()==200){
            //EntityUtils是org.apache.http.util下的一个工具类
            String s = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(s, SocialUser.class);
            //判断这个用户是否是第一次使用当前社交登录
            R r = memberFeignService.socialLogin(socialUser);

            if(r.getCode()==0){
                //登录成功
                MemberEntityVo data = r.getData(new TypeReference<MemberEntityVo>(){
                });
                log.info("登录成功：用户信息：{}",data.toString());
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
//
                return "redirect:http://gulimall.com";
            }else{
                //登录失败
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }else{
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }

    /**
     * 如果登录过了就直接跳到首页就可以了
     * @return
     */
    @GetMapping("/login.html")
    public String loginPage(HttpSession session){
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if(attribute==null){
            return "login";
        }else{
            return  "redirect:http://gulimall.com";
        }
    }

}
